﻿using System;
using System.Collections.Generic;
using System.Web;
using PayRoller.Code;
using System.Web.SessionState;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using HySoft.Cash.Business;
using HySoft.Cash.Structure;
using System.Configuration;
using System.Web.Profile;
using Huayou.UI.WebControls;
using System.Data;
using Huayou.Common;
using PayRoller.Include;
using HySoft.Cash;

namespace PayRoller.Manage.Documents
{
    public class DocList : ParentPage, IRequiresSessionState
    {
        // Fields
        protected Huayou.UI.WebControls.DataGridTable dgdList;
        protected HtmlForm form1;
        protected HtmlHead Head1;
        protected Label lblTip;
        protected TextBox txtParamstr;
        protected TextBox txtFileName;
        protected UOrgNameInput UOrgNameInput1;

        protected string DateDisplayFormat = AppSettings.DateFormatAU;

        // Methods
        protected void dgdList_DataBinding(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            TdocumentsBN sbn = new TdocumentsBN(this.Page);
            sbn.Order = "Sid desc";

            string fileNameGetted= this.txtFileName.Text.Trim();
            if (fileNameGetted != string.Empty)
            {
                sbn.QueryFilename(fileNameGetted);
            }

            int orgIDGetted = this.UOrgNameInput1.OrgIDSelected;
            if (orgIDGetted > 0)
            {
                sbn.QueryOrgID(orgIDGetted);
            }

            dt = sbn.GetList();
            sbn.Close();
            sbn.Dispose();
            CommonBasePage.SetPage(this.dgdList, dt);
            base.AddValue("pageno", Convert.ToString((int)(this.dgdList.CurrentPageIndex + 1)));
            this.txtParamstr.Text = base.PackPart(base.ParamValue);
        }

        protected void dgdList_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            this.dgdList.CurrentPageIndex = e.NewPageIndex;
            this.dgdList.DataBind();
        }

        protected override void OnInit(EventArgs e)
        {
            base.m_ModuleCode = "";
            base.CheckLogin(true);
            base.CheckPerm();
            base.OnInit(e);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                base.ParamValue = base.UnPack();
                this.txtParamstr.Text = base.PackPart(base.ParamValue);
                string str = base.GetValue("pageno");
                if (str != null)
                {
                    this.dgdList.CurrentPageIndex = Convert.ToInt32(str) - 1;
                }
                this.dgdList.DataBind();
            }
            else
            {
                base.ParamValue = base.UnPackPart(this.txtParamstr.Text);
            }
        }

        // Properties
        protected HttpApplication ApplicationInstance
        {
            get
            {
                return this.Context.ApplicationInstance;
            }
        }

        protected DefaultProfile Profile
        {
            get
            {
                return (DefaultProfile)this.Context.Profile;
            }
        }

        protected void dgdList_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            ListItemType listItemType= e.Item.ItemType;
            if (listItemType == ListItemType.AlternatingItem || listItemType == ListItemType.Item || listItemType== ListItemType.SelectedItem)
            {
                DataRowView drv = (DataRowView)e.Item.DataItem;

                HyperLink HYDownload= e.Item.FindControl("HYDownload") as HyperLink;
                if (HYDownload != null)
                {
                    HYDownload.NavigateUrl = "~/"+ ConfigurationManager.AppSettings["upPath"] + "/" + drv["Filepath"].ToString();
                }

                HyperLink HYOrgName = e.Item.FindControl("HYOrgName") as HyperLink;
                if (HYOrgName != null)
                {
                    int orgID = 0;
                    string orgName = "CommonOrg";
                    try
                    {
                        orgID = Convert.ToInt32(drv["OrgID"]);
                    }
                    catch{}
                    if (orgID > 0)
                    {
                        using (TorginfoBN orgBN = new TorginfoBN(this.Page))
                        {
                            TorginfoDT orgDT = orgBN.Get(orgID);
                            if (orgDT != null)
                            {
                                orgName = orgDT.OrgName;
                            }
                        }
                    }
                    HYOrgName.Text = orgName;
                }
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            this.dgdList.DataBind();
        }
    }

}
